package com.yaowk.sfp.utils.verification.sms.aliyun;

import com.libfintech.verification.service.ISmsService;
import com.libfintech.verification.service.Result;
import com.yaowk.sfp.utils.verification.sms.RedisRepositoryFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author 小凯
 **/
@Component
public class AliYunSmsUtils implements ISmsService {

    @Value("${yaowk.sms.aliyun.accessKeyId:}")
    private String accessKeyId;
    @Value("${yaowk.sms.aliyun.accessKeySecret:}")
    private String accessKeySecret;
    @Value("${yaowk.sms.aliyun.default.templateCode:}")
    private String templateCode;
    @Value("${yaowk.sms.aliyun.default.signName:}")
    private String signName;
    @Value("${yaowk.sms.aliyun.imageCodeDisplayCount:1000}")
    private Integer imageCodeDisplayCount;
    @Value("${yaowk.sms.aliyun.send:true}")
    private Boolean send;

    private AliYunSmsService service;

    @PostConstruct
    public void init() {
        AliYunSmsConfig config = new AliYunSmsConfig();
        config.setRepositoryConfig(RedisRepositoryFactory.getRedisRepositoryConfig());
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setImageCodeDisplayCount(imageCodeDisplayCount);
        config.setSend(send);


        service = new AliYunSmsService(config);
    }

    @Override
    public Result<String> send(String s, String s1, String s2, String s3) {
        return service.send(s, s1, s2, s3);
    }

    @Override
    public void send(String s, String s1) {
        service.send(s, s1);
    }

    @Override
    public Result<Void> verify(String s, String s1, String s2, String s3) {
        return service.verify(s, s1, s2, s3);
    }

    @Override
    public Result<String> refreshImageCode(String s, String s1) {
        return service.refreshImageCode(s, s1);
    }

    public Result<String> send(String s, String s1, String s2, String s3, String templateCode, String signName) {
        try {
            service.put(s, s2, templateCode, signName);
            return service.send(s, s1, s2, s3);
        } finally {
            service.remove(s, s2);
        }
    }
}
